package com.offcn.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.offcn.constants.MessageConstant;
import com.offcn.constants.PageResult;
import com.offcn.constants.QueryPageBean;
import com.offcn.constants.Result;
import com.offcn.pojo.Checkgroup;
import com.offcn.pojo.Checkitem;
import com.offcn.service.CheckgroupService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/checkgroup")
public class CheckgroupController {
    @Reference
    private CheckgroupService checkgroupService;

    @RequestMapping("/save")
    public Result save(Integer[] checkitemIds,@RequestBody Checkgroup checkgroup){
        for (Integer checkitemId : checkitemIds) {
            System.out.println(checkitemId);
        }
        try {
            checkgroupService.save(checkitemIds,checkgroup);
            return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_CHECKGROUP_FAIL);
        }
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = checkgroupService.findPage(queryPageBean);
        return pageResult;
    }

    @RequestMapping("/deleteById")
    public Result deleteById(Integer id){
        System.out.println(id);
        try {
            checkgroupService.deleteById(id);
            return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
    }

    @RequestMapping("/edit")
    public Result edit(Integer[] checkitemIds,@RequestBody Checkgroup checkgroup){
        try {
            checkgroupService.edit(checkitemIds,checkgroup);
            return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
    }

//    findCheckitemsById 通过检查组id获得检查项id列表
    @RequestMapping("/findCheckitemsById")
    public Result findCheckitemsById(Integer gid){
        Result result = checkgroupService.findCheckitemsById(gid);
        return result;
    }

    @RequestMapping("/findAll")
    public Result findAll(){
        Result result = checkgroupService.findAll();
        return result;
    }
}
